import greenfoot.*;  // (World, Actor, GreenfootImage, Greenfoot and MouseInfo)

/**
 * Write a description of class Enemigo here.
 * 
 * @author (your name) 
 * @version (a version number or a date)
 */

public class Enemigo extends Actor
{
    
    public int disparo=1;
    public int dir=2,count;
    public int aux2=0;
    final int jSpeed = 20; // el primer "salto" de velocidad
    int ySpeed = 0, xSpeed = 0; // las velocidades iniciales verticales y horizontales
    boolean aboutFace; // la dirección (izquierda o derecha) el actor se enfrenta
    boolean onGround; // el estado del actor se establece en un objeto o no
    public int mover1(int step){
        int auxX=getX();
            if(step<150){
                auxX=getX()+2;
                dir=1;
                
            }
            else{
            if(step<300){
                auxX=getX()-2;
                dir=2;
            }
            else
            return 0;}
            setLocation(auxX,getY());
            step++;
            return step;
    }
    
    /**
     * Ira disparando los enemigos
     */public void Dispara(int dir, int gun)
    {
        int x=Greenfoot.getRandomNumber(30);
        if(gun==4)
            x=Greenfoot.getRandomNumber(200);
        if(x==29)
        {
            switch(gun){
                case 1:
                    getWorld().addObject(new bullete1(dir),getX(),getY());
                break;
                case 2: 
                if(dir==1){
                    getWorld().addObject(new bullete1(dir),getX(),getY());
                    getWorld().addObject(new bullete1(dir),getX()+50,getY());
                    getWorld().addObject(new bullete1(dir),getX()+75,getY());
                }
                else
                {
                    getWorld().addObject(new bullete1(dir),getX(),getY());
                    getWorld().addObject(new bullete1(dir),getX()-50,getY());
                    getWorld().addObject(new bullete1(dir),getX()-75,getY());
                }
                break;
                case 3:
                    getWorld().addObject(new bullete1(dir),getX(),getY()-10);
                    getWorld().addObject(new bullete1(dir),getX(),getY());
                    getWorld().addObject(new bullete1(dir),getX(),getY()+10);
                break;
                case 4:
                     getWorld().addObject(new bulletGE(dir),getX(),getY());
                break;
            }
    
        }
        }
        
    public void mover() 
    {
        ySpeed++; // suma gravedad
        //getWorld().addObject(new Rex2(),getX(),getY());
        onGround=false;
        setLocation(getX()+xSpeed/10, getY()+ySpeed);
        Actor bal1=getOneIntersectingObject(bullete1.class);
        Actor bal2=getOneIntersectingObject(bulletGE.class);
        Actor bal3= getOneIntersectingObject(bullet1.class);
        Actor bal4 = getOneIntersectingObject(bulletG.class);
            while(getOneObjectAtOffset(0, getImage().getHeight()/2+1, null)!=null)
            {
                setLocation(getX(), getY()-1); 
                onGround=true; 
                ySpeed=0;
            }
       
            if(bal1==null&&bal2==null&&bal3==null&&bal4==null){
            //comprobar por encima del actor
            while(getOneObjectAtOffset(0, -getImage().getHeight()/2-1, null)!=null) 
            {
                setLocation(getX(), getY()+1);
                onGround=true;
                ySpeed = 0;
            }
           // verifique derecho del actor
            
           while(getOneObjectAtOffset(getImage().getWidth()/2+1, 0, null)!=null)
            {   
                setLocation(getX()-1, getY());
                onGround=true;
                xSpeed = 0;
            }
            //verificación a la izquierda del actorr
            while(getOneObjectAtOffset(-getImage().getWidth()/2-1, 0, null)!=null)
            {   
                setLocation(getX()+1, getY());
                onGround=true;
                xSpeed = 0;
            }
        }
       
       
    }    
    public int getdireccion(int c){
         MyWorld w = (MyWorld)getWorld();
         if (aux2>w.getjugx()+1)
        {
            if(c==0||c==2||c==3)
                c=1;
            else
                c=0;
            
            aux2=getX()-2;
            setLocation(aux2,getY());
            xSpeed=-1;
        }
        
        if (aux2<w.getjugx()+1)
        {
            if(c==0||c==1||c==3)
                c=2;
            else
               c=3;
              
            xSpeed=+1;
            aux2=getX()+2;
             setLocation(aux2,getY());
        }
        Actor wall=getOneIntersectingObject(Pared.class);
        if (wall!=null && onGround ) // comprobar brinco
        {
            if(ySpeed<200)
            {
            ySpeed=0;
            ySpeed -= jSpeed;//añadir saltar velocidad
            }
        
            
        }
        return c;
    }
    int hit(){
        int h=0;
        Actor bal = getOneIntersectingObject(bullet1.class);
        Actor bal2 = getOneIntersectingObject(bulletG.class);
        if (bal!=null)
        {
            getWorld().removeObject(bal);
            h=5;
        }
        if (bal2!=null)
        {
            getWorld().removeObject(bal2);
            getWorld().addObject( new explosion(), getX(), getY() );
            h=51;
        }
        return(h);
    }
    
}
